package com.oig.sys.base.service.impl;

import com.oig.sys.base.dao.ITbInnerAddressDao;
import com.oig.sys.base.entity.TbInnerAddress;
import com.oig.sys.base.service.ITbInnerAddressService;

import com.oig.sys.base.vo.InnerAddressVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuxl
 */
@Slf4j
@Service
public class TbInnerAddressServiceImpl implements ITbInnerAddressService {

	private final ITbInnerAddressDao tbInnerAddressDao;

	@Autowired
	public TbInnerAddressServiceImpl(ITbInnerAddressDao tbInnerAddressDao){
		 this.tbInnerAddressDao = tbInnerAddressDao;
	}


	@Override
	public List<InnerAddressVo> findTopInnerAddress(String searchWord) {
		if (searchWord==null){
			searchWord = "";
		}
		log.debug("query top ten inner address searchWord={}", searchWord);
		List<TbInnerAddress> innerAddressList = this.tbInnerAddressDao.findTopTen(searchWord, PageRequest.of(0, 10));
		List<InnerAddressVo> voList = new ArrayList<>();
		if(innerAddressList != null && innerAddressList.size() > 0){
			innerAddressList.forEach(innerAddress ->{
				InnerAddressVo vo = new InnerAddressVo();
				vo.setCode(innerAddress.getCode());
				vo.setName(innerAddress.getName());
				voList.add(vo);
			} );
		}
		return voList;
	}

}
